package jphysics;

import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;

/**
 * @author KaLeonis
 */
public class DRectangle extends Group {
	
	private Rectangle r0;
	private Circle c1;
	
	public DRectangle() {
		
		r0 = new Rectangle(170, 60, 50, 50);
		c1 = new Circle(r0.getX() + r0.getWidth(), r0.getY() + r0.getHeight(), 5);
		
		r0.setFill(Color.BURLYWOOD);
		c1.setFill(Color.BLACK);
		
		r0.setOnMouseDragged(new EventHandler<MouseEvent>() {
			public void handle(MouseEvent t) {
				r0_onMouseDragged(t);
			}
		});
		
		c1.setOnMouseDragged(new EventHandler<MouseEvent>() {
			public void handle(MouseEvent t) {
				c1_onMouseDragged(t);
			}
		});
		
		getChildren().add(r0);
		getChildren().add(c1);
	}
	
	private void r0_onMouseDragged(MouseEvent t) {
		r0.setX(t.getX());
		r0.setY(t.getY());
		c1.setCenterX(r0.getX() + r0.getWidth());
		c1.setCenterY(r0.getY() + r0.getHeight());
	}

	private void c1_onMouseDragged(MouseEvent t) {
		c1.setCenterX(t.getX());
		c1.setCenterY(t.getY());
		r0.setWidth(c1.getCenterX() - r0.getX());
		r0.setHeight(c1.getCenterY() - r0.getY());
	}
}